package com.lora.dao.impl;

import com.acooly.core.utils.StringUtils;
import com.lora.dao.EquimentDao;
import com.lora.util.StringUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Repository
public class EquimentDaoImpl implements EquimentDao {
    @Resource
    JdbcTemplate jdbcTemplate;
    public List<Map<String,Object>> query(Map<String,Object> map){

        StringBuilder sql=new StringBuilder();
        sql.append("select id,type,eid,name as ename,position,parameter  from equipent where 1=1 ");
        String eid=(String)map.get("eid");
        String id=(String)map.get("id");
        String type=(String)map.get("type");
        String method=(String)map.get("method");
        if(!StringUtils.isEmpty(eid)){
            sql.append(" and eid='"+eid+"'");
        }
        if(!StringUtils.isEmpty(type)){
            sql.append(" and type="+type+"");
        }
        if(!StringUtils.isEmpty(id)&&!id.equals("undefined")){
            sql.append(" and id="+id);
        }
        if(!StringUtils.isEmpty(method)){
            sql.append(" and scheduled_method='"+method+"'");
        }
        sql.append(" order by eid asc");
        return jdbcTemplate.queryForList(sql.toString());
    }
    public List<Map<String,Object>> queryalarm(Map<String,Object> map){
        String equipmenttype=(String)map.get("equipmenttype");
        StringBuilder sqlwhere=new StringBuilder();
        if(!StringUtils.isEmpty(equipmenttype)){
            sqlwhere.append(" and equipmenttype="+equipmenttype);
        }
        return jdbcTemplate.queryForList("select*from alarm where 1=1 ");
    }
    public List<Map<String,Object>> queryalarmParment(Map<String,Object> map){
        String equipmenttype=(String)map.get("equipmenttype");
        String id=(String)map.get("id");
        StringBuilder sqlwhere=new StringBuilder();
        if(!StringUtils.isEmpty(equipmenttype)){
            sqlwhere.append(" and equipmenttype="+equipmenttype);
        }
        if(!StringUtils.isEmpty(id)){
            sqlwhere.append(" and id="+id);
        }
        return jdbcTemplate.queryForList("select*from alarm_parameter where 1=1 "+sqlwhere);
    }
    public int saveAlarmParment (Map<String,Object> map){
        int i=jdbcTemplate.update("INSERT INTO alarm_parameter ( `equipmenttype`,`equipmentname`,parameter) VALUES (?,?,?)",map.get("equipmenttype"),map.get("equipmentname"),map.get("parameter"));
        return i;
    }
    public int editAlarmParment (Map<String,Object> map){
        int i=jdbcTemplate.update("UPDATE `alarm_parameter` SET  `equipmentname`=?, `parameter`=?, `equipmenttype`=? WHERE (`id`=?)",map.get("equipmentname"),map.get("parameter"),map.get("equipmenttype"),map.get("id"));
        return i;
    }
    public int alarmdelete(String id){
        return jdbcTemplate.update("DELETE from alarm where id="+id);
    }
    public int delete(String id){
        return jdbcTemplate.update("DELETE from equipent where id="+id);
    }
    public int alarmParmentdelete(String id){
        return jdbcTemplate.update("DELETE from alarm_parameter where id="+id);
    }
    public int saveAlarm (Map<String,Object> map){
        int i=jdbcTemplate.update("INSERT INTO alarm ( `equipmenttype`,`equipmentname`,parment, `value`,remarks) VALUES (?,?,?,?,?)",map.get("equipmenttype"),map.get("equipmentname"),map.get("parment"),map.get("value"),map.get("remarks"));
        return i;
    }
    public int save (Map<String,Object> map){
        int i=jdbcTemplate.update("INSERT INTO equipent ( `type`,name, `eid`,position,parameter) VALUES (?,?,?,?,?)",map.get("type"),map.get("name"),map.get("eid"),map.get("position"),map.get("parameter"));
        return i;
    }
    public int update (Map<String,Object> map){
        int i=jdbcTemplate.update("UPDATE `equipent` SET  `type`=?, `name`=?, `eid`=?, `position`=?, `parameter`=? WHERE (`id`=?)",map.get("type"),map.get("name"),map.get("eid"),map.get("position"),map.get("parameter"),map.get("id"));
        return i;
    }
}
